/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import javax.xml.bind.JAXBException;
import myconfession.dao.UserPageDAO;
import myconfession.dto.userpage.UserPageList;
import myconfession.utils.ConstantProvider;
import myconfession.utils.XMLUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.math.BigInteger;
import myconfession.dao.AccountDAO;
import myconfession.dto.page.Page;
import myconfession.dto.userpage.UserPage;

/**
 *
 * @author Liem
 */
public class UserPageBO {

    public static void loadAllAndMarshall(String filePath) throws JAXBException {
        UserPageList list = UserPageDAO.getAllUserPage();
        XMLUtility.marshallJAXB(filePath, list);
    }

    public static void addUserPage(UserPage up) throws JAXBException {
        UserPageList userPageList = (UserPageList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), UserPageList.class);
        userPageList.getUserPage().add(up);
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), userPageList);
    }

    public static void setUserPageStatus(int pageId, boolean isActive) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException {
        Document dom = XMLUtility.parseDOMfromFile(ConstantProvider.getInstance().getUserpageXmlPath());
        setUserPageStatus(dom, pageId, isActive);
        XMLUtility.writeDOMtoXml(dom, ConstantProvider.getInstance().getUserpageXmlPath());
    }

    public static void setUserPageStatus(Node node, int pageId, boolean isActive) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("pageId")) {
            if (node.getTextContent().trim().equals(Integer.toString(pageId))) {
                Node parent = node.getParentNode();
                NodeList child = parent.getChildNodes();
                for (int i = 0; i < child.getLength(); i++) {
                    Node tmp = child.item(i);
                    if (tmp.getNodeName().equals("isActive")) {
                        if (isActive) {
                            tmp.setTextContent("true");
                        } else {
                            tmp.setTextContent("false");
                        }
                        return;
                    }
                }
            }
        }
        NodeList child = node.getChildNodes();
        for (int i = 0; i < child.getLength(); i++) {
            setUserPageStatus(child.item(i), pageId, isActive);
        }
    }

    public static void addUserToPage(String user, int pageId) throws JAXBException {
        if (AccountDAO.checkExistUser(user)) {
            if (!UserPageDAO.checkExistUserOnPage(pageId, user)) {
                Page page = PageBO.getPageById(new BigInteger(pageId + ""));
                UserPage us = new UserPage();
                us.setUsername(user);
                us.setPageId(new BigInteger(pageId + ""));
                us.setRole("manager");
                us.setDescription(page.getDescription());
                us.setPagename(page.getPagename());
                us.setIsActive(page.isIsActive());
                us.setFbPageId(page.getFbPageId());
                if (UserPageDAO.insertUserPage(us)) {
                    UserPageBO.addUserPage(us);
                }
            }
        }
    }

    public static void removeUserToPage(String user, int pageId) throws JAXBException {
        UserPageList userPageList = (UserPageList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), UserPageList.class);
        for (UserPage tmp : userPageList.getUserPage()) {
            if (tmp.getUsername().trim().equals(user) && tmp.getPageId().intValue() == pageId) {
                if (UserPageDAO.deleteUserPage(tmp)) {
                    userPageList.getUserPage().remove(tmp);
                    break;
                }
            }
        }
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), userPageList);
    }
}
